import { RouteRecord } from 'vue-router'
import Layout from '@/layout/index.vue'
import { chdir } from 'process'
/**
 * 格式化两个时间间隔：天、时、分、秒
 * @param startTime 开始时间
 * @param endTime 结束时间
 */
export const twoTimeInterval = (startTime: string, endTime: string | Date) => {
    //console.log(startTime, endTime)
    // 开始时间
    //let d1 = startTime.replace(/\-/g, '/')

    let date1 = new Date(startTime)

    // 结束时间
    //let d2 = endTime.replace(/\-/g, "/");

    let date2 = endTime instanceof Date ? endTime : new Date(endTime)
    // 时间相差秒数
    let dateDiff = date2.getTime() - date1.getTime()

    // 计算出相差天数
    let days = Math.floor(dateDiff / (24 * 3600 * 1000))

    // 计算出小时数
    let residue1 = dateDiff % (24 * 3600 * 1000) // 计算天数后剩余的毫秒数

    let hours = Math.floor(residue1 / (3600 * 1000))

    // 计算相差分钟数
    let residue2 = residue1 % (3600 * 1000) // 计算小时数后剩余的毫秒数
    let minutes = Math.floor(residue2 / (60 * 1000))

    // 计算相差秒数
    let residue3 = residue2 % (60 * 1000) // 计算分钟数后剩余的毫秒数
    let seconds = Math.round(residue3 / 1000)

    let returnVal =
        (days == 0 ? '' : days + '天') +
        (hours == 0 ? '' : hours + '时') +
        (minutes == 0 ? '' : minutes + '分') +
        (seconds == 0 ? '' : seconds + '秒')

    return returnVal
}
/**
 * 公用菜单
 */
export const publicRoutes = () => {
    return [{
        path: '/',
        component: Layout,
        redirect: '/dashboard',
        children: [
            {
                path: 'dashboard',
                component: () => import(/* webpackChunkName: "dashboard" */ '@/views/dashboard/index.vue'),
                meta: {
                    title: '主页',
                    icon: 'dashboard',
                    ishome: true
                }
            }
        ]
    }, {
        path: '*',
        redirect: '/404',
        meta: { hidden: true }
    }]
}
/**
 * 生成路由
 * @param menuTree 
 */
export const generRoutes = (menuTree: [], root: any[]) => {

    menuTree.forEach((node: any) => {
        var it = node.node;
        var r: any = {};
        r.path = it.path;
        if (it.component) {
            r.component = () => import('@/views' + it.component)
        } else {
            r.component = Layout;
        }
        
        r.meta = {
            title: it.title,
            hidden: it.hidden,
            icon: it.icon
        }
        if(it.perbtns){
            r.meta.btns=eval('('+it.perbtns+')');
        }
        if (it.redirect) r.redirect = it.redirect;
        if (node.children && node.children.length > 0) {
            r.children = [];
            generRoutes(node.children, r.children);
        }

        root.push(r);
    });
}